package com.xcw.jac.deoc;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Main {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, IOException {
        List<Person> personList = new ArrayList<>();

        Random random = new Random();

        for (int i = 0; i < 10; i++) {
            Person person = new Person();
            person.setName(generateRandomString(5));
            person.setAge(random.nextInt(100));
            person.setGender(random.nextBoolean() ? "Male" : "Female");
            person.setAddress(generateRandomString(10));
            person.setPhoneNumber(generateRandomString(8));
            person.setEmail(generateRandomString(8) + "@example.com");
            person.setOccupation(generateRandomString(7));
            person.setEducation(generateRandomString(7));
            person.setNationality(generateRandomString(5));
            person.setMaritalStatus(random.nextBoolean() ? "Married" : "Single");
            person.setBloodType(generateRandomString(2));
            person.setFavoriteColor(generateRandomString(6));
            person.setHobby(generateRandomString(10));
            person.setBirthPlace(generateRandomString(10));
            person.setReligion(generateRandomString(8));
            personList.add(person);
        }

//
//
//        Field[] fields = person.getClass().getDeclaredFields();
//        String[] arr = new String[fields.length];
//
//        for (Field f : fields) {
//            MyExcel annotation = f.getAnnotation(MyExcel.class);
//            arr[annotation.index()] = annotation.value();
//            System.out.println(annotation.index());
//            System.out.println(annotation.value());
//            String fieldName = f.getName();
//            String getterMethodName = "get" + capitalize(fieldName);
//
//            Method getterMethod = Person.class.getMethod(getterMethodName);
//            Object value = getterMethod.invoke(person);
//
//            System.out.println(fieldName + ": " + value);
//        }
//        System.out.println(Arrays.toString(arr));
        long l = System.currentTimeMillis();
        String sheetName = "学生成绩信息统计表s";// sheet名
        String path = System.getProperty("user.dir");
        String createPath = path + "\\src\\main\\resources\\excel\\" + sheetName + ".xls";
        System.out.println("E:\\idea\\bocloud\\fszw\\javac\\src\\main\\resources\\excel");
        System.out.println(createPath);
        FileOutputStream fos=new FileOutputStream(createPath);

        HSSFWorkbook wb = new HSSFWorkbook();
        MyExcelUtils.excel2Data(wb, personList);
        wb.write(fos);
        fos.close();
        long currentTimeMillis = System.currentTimeMillis();
        System.out.println(currentTimeMillis - l);
    }


    private static String generateRandomString(int length) {
        String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        for (int i = 0; i < length; i++) {
            int index = random.nextInt(characters.length());
            sb.append(characters.charAt(index));
        }

        return sb.toString();
    }

    private static String capitalize(String str) {
        if (str == null || str.isEmpty()) {
            return str;
        }

        return str.substring(0, 1).toUpperCase() + str.substring(1);
    }
}

